<template>
  <div class="bill-list-container">
    <el-card class="table-card">
      <template #header>
        <div class="table-header">
          <h3>账单管理</h3>
          <div class="search-toggle-area">
            <el-button class="search-toggle-button" @click="toggleSearchBar">
              <el-icon><Search /></el-icon>
            </el-button>
            <el-form :inline="true" :model="queryParams" class="search-form" :class="{'is-hidden': !showSearchBar}" ref="queryForm">
              <el-form-item label="支付状态">
                <el-select v-model="queryParams.paymentStatus" placeholder="请选择状态" clearable style="width: 160px;">
                  <el-option v-for="item in statusOptions" :key="item.value" :label="item.label" :value="item.value"></el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="支付方">
                <el-input v-model="queryParams.payerName" placeholder="请输入支付方名称" clearable style="width: 160px;" />
              </el-form-item>
              <el-form-item label="账单类型">
                <el-select v-model="queryParams.billType" placeholder="请选择类型" clearable style="width: 160px;">
                  <el-option v-for="item in billTypeOptions" :key="item.value" :label="item.label" :value="item.value"></el-option>
                </el-select>
              </el-form-item>
              <el-form-item>
                <el-button type="primary" @click="handleQuery">查询</el-button>
                <el-button @click="resetQuery">重置</el-button>
              </el-form-item>
            </el-form>
          </div>
        </div>
      </template>

      <el-table :data="billList" style="width: 100%" v-loading="loading">
        <el-table-column prop="billNumber" label="账单编号" width="200" />
        <el-table-column prop="payerName" label="支付方" width="180" />
        <el-table-column prop="amount" label="金额(元)" />
        <el-table-column prop="dueDate" label="应付日期" />
        <el-table-column prop="billType" label="账单类型" />
        <el-table-column prop="paymentStatus" label="支付状态">
          <template #default="{ row }">
            <el-tag :type="getStatusType(row.paymentStatus)">{{ row.paymentStatus }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template #default="{ row }">
            <el-button type="text" @click="viewDetails(row.id)">查看详情</el-button>
          </template>
        </el-table-column>
      </el-table>

      <el-pagination
        v-if="total > 0"
        class="pagination"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pagination.current"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="pagination.size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
    </el-card>
  </div>
</template>

<script setup>
import { ref, onMounted, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { getBillList } from '@/api/finance'
import { ElMessage } from 'element-plus'
import { Search } from '@element-plus/icons-vue'

const router = useRouter()
const billList = ref([])
const loading = ref(true)
const total = ref(0)
const showSearchBar = ref(false)

const statusOptions = ref([
  { value: '已支付', label: '已支付' },
  { value: '未支付', label: '未支付' },
  { value: '逾期', label: '逾期' },
  { value: '已关闭', label: '已关闭' },
  { value: '部分支付', label: '部分支付' }
])

const billTypeOptions = ref([
  { value: '租金', label: '租金' },
  { value: '物业费', label: '物业费' },
  { value: '水电费', label: '水电费' },
  { value: '停车费', label: '停车费' },
  { value: '其他', label: '其他' }
])

const queryParams = reactive({
  current: 1,
  size: 10,
  paymentStatus: '',
  payerName: '',
  billType: ''
})

const pagination = reactive({
    current: 1,
    size: 10
})

const fetchBillList = async () => {
  try {
    loading.value = true
    const params = {
        current: pagination.current,
        size: pagination.size,
        paymentStatus: queryParams.paymentStatus,
        payerName: queryParams.payerName,
        billType: queryParams.billType
    }
    const res = await getBillList(params)
    if (res.data.code === 200) {
        billList.value = res.data.data.list
        total.value = res.data.data.total
    } else {
        ElMessage.error(res.data.message || '获取账单列表失败')
    }
  } catch (error) {
    ElMessage.error('获取账单列表失败')
    console.error(error)
  } finally {
    loading.value = false
  }
}

const handleSizeChange = (val) => {
    pagination.size = val
    fetchBillList()
}

const handleCurrentChange = (val) => {
    pagination.current = val
    fetchBillList()
}

// 切换搜索栏显示状态
const toggleSearchBar = () => {
  showSearchBar.value = !showSearchBar.value
}

// 查询
const handleQuery = () => {
  pagination.current = 1
  fetchBillList()
}

// 重置
const resetQuery = () => {
  queryParams.paymentStatus = ''
  queryParams.payerName = ''
  queryParams.billType = ''
  handleQuery()
}

const getStatusType = (status) => {
  switch (status) {
    case '已支付': return 'success';
    case '未支付': return 'warning';
    case '逾期': return 'danger';
    case '已关闭':
    case '部分支付':
      return 'info';
    default: return '';
  }
}

const viewDetails = (id) => {
  router.push({ name: 'BillDetail', params: { id } })
}

onMounted(() => {
  fetchBillList()
})
</script>

<style scoped>
.bill-list-container {
  padding: 20px;
  background-color: #f8f9fa;
  min-height: calc(100vh - 80px);
}

.table-card {
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgba(0,0,0,0.05);
}

.table-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.table-header h3 {
  margin: 0;
  font-size: 18px;
  font-weight: 600;
}

/* 搜索区域 */
.search-toggle-area {
  display: flex;
  flex-direction: row;
  align-items: center;
}

.search-toggle-button {
  font-size: 20px;
  color: #606266;
  align-self: center;
  margin-right: 15px;
  transition: all 0.2s;
  cursor: pointer;
  width: 36px;
  height: 36px;
  display: flex;
  justify-content: center;
  align-items: center;
  border-radius: 50%;
  background-color: rgba(0, 0, 0, 0.03);
  border: 1px solid rgba(0, 0, 0, 0.05);
  z-index: 10;
  margin-bottom: 0;
}

.search-toggle-button:hover {
  color: #409eff;
  background-color: rgba(64, 158, 255, 0.1);
  border-color: rgba(64, 158, 255, 0.2);
}

.search-form {
  max-width: 1000px;
  width: auto;
  overflow: hidden;
  transition: max-width 0.4s ease-in-out, opacity 0.4s ease-in-out, background-color 0.4s ease-in-out, box-shadow 0.4s ease-in-out, padding 0.4s ease-in-out, margin-left 0.4s ease-in-out, height 0.4s ease-in-out, min-height 0.4s ease-in-out;
  opacity: 1;
  background-color: transparent;
  border-radius: 8px;
  padding: 0 20px;
  box-shadow: none;
  pointer-events: auto;
  margin-left: 15px;
  white-space: nowrap;
  height: 35px;
  min-height: 35px;
}

.search-form.is-hidden {
  max-width: 0;
  opacity: 0;
  padding: 0;
  margin-left: 0;
  height: 0;
  min-height: 0;
  background-color: transparent;
  box-shadow: none;
  pointer-events: none;
}

/* 调整搜索表单项的底部外边距 */
:deep(.search-form .el-form-item) {
  margin-bottom: 0;
  margin-right: 15px;
  display: inline-flex;
  align-items: center;
}

:deep(.search-form .el-form-item__label) {
  padding-right: 8px;
}

:deep(.table-card .el-card__header) {
  background-color: #f9fafc;
  padding: 15px 20px;
}

:deep(.table-card .el-card__body) {
  padding: 20px;
}

.pagination {
  margin-top: 25px;
  display: flex;
  justify-content: flex-end;
  padding: 12px 15px;
  background-color: #f9fbfd;
  border-radius: 10px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.03);
}

/* 搜索框el-input优化 */
:deep(.el-input__inner) {
  border-color: #e0e5ed !important;
  transition: border-color 0.2s cubic-bezier(0.645, 0.045, 0.355, 1);
  border-radius: 6px;
}

:deep(.el-input__inner:focus) {
  border-color: rgba(64, 158, 255, 0.4) !important;
}

:deep(.el-input__inner::placeholder) {
  color: #aebccf;
}

/* 按钮el-button优化 */
:deep(.el-button--primary) {
  background-color: rgba(64, 158, 255, 0.1) !important;
  color: #606266 !important;
  border-color: rgba(64, 158, 255, 0.2) !important;
  transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1);
  border-radius: 6px;
}

:deep(.el-button--primary:hover) {
  background-color: rgba(64, 158, 255, 0.15) !important;
  border-color: rgba(64, 158, 255, 0.3) !important;
}

/* Default Button */
:deep(.el-button--default) {
  background-color: #ffffff !important;
  color: #606266 !important;
  border-color: #dcdfe6 !important;
  transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1);
  border-radius: 6px;
}

:deep(.el-button--default:hover) {
  background-color: #f7f9fa !important;
  border-color: #c8d3dd !important;
}
</style> 